package edu.hawaii.ics414.blackjack.tournamentMode;

import edu.hawaii.ics414.blackjack.card.*;
import edu.hawaii.ics414.blackjack.player.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Random;    
/**
 * Tournament Mode for the BlackJack Game.
 * 
 * @author Wei Wang
 *
 */
public class tournamentMode 
{
  /**
   * data fields
   */
  Scanner scan = new Scanner(System.in);
  private GameDeck deck;
  private AIPlayer cpu1;
  private AIPlayer cpu2;
  private AIPlayer cpu3;
  private AIPlayer cpu4;
  boolean cpu1Status;
  boolean cpu2Status;
  boolean cpu3Status;
  boolean cpu4Status;
  private Player player;
  private Dealer dealer;
  private int rand;
  private Random generator;
  private int numDecks;
  private int startCash;
  private String spacer;
  private String Choice;
  private int playerBet;
  /**
   * A constructor for the tournament mode
   */
  public tournamentMode() 
  {
    numDecks = 6;
    deck = new GameDeck(numDecks);
    generator = new Random();
    startCash = 2000;
  }
  /**
   * The main tournament mode
   * @param level
   * @return
   */
  public boolean Tournament(int level)
  {
    /*
     * Welcome messages
     */
    switch(level)
    {
    case 1:
      System.out.println("Welcome to round 1 of tournament mode!");
      break;
    case 2:
      System.out.println("Welcome to round 2 of tournament mode!");
      break;
    case 3:
      System.out.println("Welcome to round 3 of tournament mode!");
    }
    System.out.println("All players on the table starts with $2000");
    System.out.println("Good Luck!");
    System.out.print("Enter anything to continue.");
    Choice = scan.nextLine();
    /*
     * Initializing the dealer, player, AIs
     */
    System.out.println("----------------------------------------");
    System.out.print("AI1 - level: ");
    cpu1 = AIGenerator(level);
    cpu1Status = true;
    System.out.print("AI2 - level: ");
    cpu2 = AIGenerator(level);
    cpu2Status = true;
    System.out.print("AI3 - level: ");
    cpu3 = AIGenerator(level);
    cpu3Status = true;
    System.out.print("AI4 - level: ");
    cpu4 = AIGenerator(level);
    cpu4Status = true;
    player = new Player(2000);
    dealer = new Dealer(0);
    System.out.println("----------------------------------------");
    System.out.print("Enter anything to continue.");
    Choice = scan.nextLine();
    /*
     * each level of the tournament lasts 16 rounds
     */
    for(int round = 1; round < 16; round++)
    {
      //if there is not enough cards, create a new deck and shuffle
      if(deck.getSize() <= 105){
        deck = new GameDeck(numDecks);
      }
      /*
       * new round
       */
      System.out.println("----------------------------------------");
      System.out.println("                Hand " + round);
      System.out.println("");
      DisplayMoney();
      System.out.println("----------------------------------------");
      System.out.print("Enter anything to continue.");
      Choice = scan.nextLine();
      /*
       * Betting
       */
      System.out.println("----------------------------------------");
      System.out.println("                Betting ");
      if(cpu1Status)
      {
        AIBetting(cpu1);
        System.out.println("AI1 bets " + cpu1.getBet(0) + ".");
      }
      if(cpu2Status)
      {
        AIBetting(cpu2);
        System.out.println("AI2 bets " + cpu2.getBet(0) + ".");
      }
      if(cpu3Status)
      {
        AIBetting(cpu3);
        System.out.println("AI3 bets " + cpu3.getBet(0) + ".");
      }
      if(cpu4Status)
      {
        AIBetting(cpu4);
        System.out.println("AI4 bets " + cpu4.getBet(0) + ".");
      }
      PlayerBetting(player);
      System.out.println("Your bid: " + player.getBet(0) + ".");
      System.out.println("----------------------------------------");
      System.out.print("Enter anything to continue.");
      Choice = scan.nextLine();
      System.out.println("");
      System.out.println("----------------------------------------");
      //Dealing
      Dealing();
      System.out.println("                Dealing");
      DisplayHands();
      System.out.println("----------------------------------------");
      System.out.print("Enter anything to continue.");
      Choice = scan.nextLine();
      System.out.println("");
      /*
       * AI turns
       */
      //cpu1 Turn
      if(cpu1Status)
      {
        System.out.println("----------------------------------------");
        System.out.println("                AI 1's Turn");
        System.out.println(cpu1.getCards());
        AISplit(deck, dealer, cpu1);
        for(int i = 0; i < 4; i++)
        {
          if(cpu1.hands[i][0] != null) 
          {
            System.out.println("cpu1 hand" + i);
            AITurn(deck, cpu1, dealer, i);
          }
        }
        System.out.println("");
        System.out.println(cpu1.getCards());
        System.out.println("----------------------------------------");
        System.out.print("Enter anything to continue.");
        Choice = scan.nextLine();
        System.out.println("");
      }
      //cpu2 Turn
      if(cpu2Status)
      {
        System.out.println("----------------------------------------");
        System.out.println("                AI 2's Turn");
        System.out.println(cpu2.getCards());
        AISplit(deck, dealer, cpu2);
        for(int i = 0; i < 4; i++)
        {
          if(cpu2.hands[i][0] != null) 
          {
            System.out.println("cpu2 hand" + i);
            AITurn(deck, cpu2, dealer, i);
          }
        }
        System.out.println("");
        System.out.println(cpu2.getCards());
        System.out.println("----------------------------------------");
        System.out.print("Enter anything to continue.");
        Choice = scan.nextLine();
        System.out.println("");
      }
      //cpu3 Turn
      if(cpu3Status)
      {
        System.out.println("----------------------------------------");
        System.out.println("                AI 3's Turn");
        System.out.println(cpu3.getCards());
        AISplit(deck, dealer, cpu3);
        for(int i = 0; i < 4; i++)
        {
          if(cpu3.hands[i][0] != null) 
          {
            System.out.println("cpu3 hand" + i);
            AITurn(deck, cpu3, dealer, i);
          }
        }
        System.out.println("");
        System.out.println(cpu3.getCards());
        System.out.println("----------------------------------------");
        System.out.print("Enter anything to continue.");
        Choice = scan.nextLine();
        System.out.println("");
      }
      //cpu4 Turn
      if(cpu4Status)
      {
        System.out.println("----------------------------------------");
        System.out.println("                AI 4's Turn");
        System.out.println(cpu4.getCards());
        AISplit(deck, dealer, cpu4);
        for(int i = 0; i < 4; i++)
        {
          if(cpu4.hands[i][0] != null) 
          {
            System.out.println("cpu4 hand" + i);
            AITurn(deck, cpu4, dealer, i);
          }
        }
        System.out.println("");
        System.out.println(cpu4.getCards());
        System.out.println("----------------------------------------");
        System.out.print("Enter anything to continue.");
        Choice = scan.nextLine();
        System.out.println("");
      }
      /*
       * Your Turn
       */
      System.out.println("----------------------------------------");
      System.out.println("                Your Turn");
      System.out.println(player.getCards());
      PlayerSplit(deck, dealer, player);
      for(int i = 1; i < 5; i++)
      {
        if(player.hands[i-1][0] != null) 
        {
          PlayerTurn(deck, player, i);
        }
      }
      System.out.println("");
      System.out.println(player.getCards());
      System.out.println("----------------------------------------");
      System.out.print("Enter anything to continue.");
      Choice = scan.nextLine();
      System.out.println("");
      /*
       * Dealer's Turn  
       */
      System.out.println("----------------------------------------");
      DisplayHands();
      System.out.println("----------------------------------------");
      System.out.print("Enter anything to continue.");
      Choice = scan.nextLine();
      System.out.println("");
      System.out.println("----------------------------------------");
      System.out.println("                Dealer's Turn");
      DealerTurn(dealer);
      System.out.println("----------------------------------------");
      /*
       * Payouts
       */
      System.out.println("                   Payout");
      System.out.println("----------------------------------------");
      if(cpu1Status)
      {
        System.out.println("AI 1 Payouts: ");
        AIPayout(cpu1, dealer, 1);
      }
      if(cpu2Status)
      {
        System.out.println("AI 2 Payouts: ");
        AIPayout(cpu2, dealer, 2);
      }
      if(cpu3Status)
      {
        System.out.println("AI 3 Payouts: ");
        AIPayout(cpu3, dealer, 3);
      }
      if(cpu4Status)
      {
        System.out.println("AI 4 Payouts: ");
        AIPayout(cpu4, dealer, 4);
      }
      System.out.println("Your Payouts: ");
      PlayerPayout(player, dealer);
      //clear the table
      ClearTable();
      /*
       * checks if any AI players are out of cash
       */
      if(cpu1.getMoney() < 10)
      {
        System.out.println("cpu1 is eliminated");
        cpu1Status = false;
      }
      if(cpu2.getMoney() < 10)
      {
        System.out.println("cpu2 is eliminated");
        cpu2Status = false;
      }
      if(cpu3.getMoney() < 10)
      {
        System.out.println("cpu3 is eliminated");
        cpu3Status = false;
      }
      if(cpu4.getMoney() < 10)
      {
        System.out.println("cpu4 is eliminated");
        cpu4Status = false;
      }
      //check if the player is out of cash
      if(player.getMoney() < 10)
      {
        System.out.println("You are eliminated on round " + level + ".");
        System.out.println("Thanks for playing");
        System.out.println("GameOver.");
        round = 20;
      }
    }
    
    if(player.getMoney() > cpu1.getMoney() 
        && player.getMoney() > cpu1.getMoney() 
        && player.getMoney() > cpu1.getMoney() 
        && player.getMoney() > cpu1.getMoney())
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  /**
   * A method that generates the AIs
   * @param level
   * @return
   */
  public AIPlayer AIGenerator(int level) 
  {
      rand = generator.nextInt(10) + 1;
      if(level == 1)
      {
        if(rand <= 5)
        {
          System.out.println("Novice");
          return new NovicePlayer(startCash);
        }
        else if(rand >= 6 && rand <= 8)
        {
          System.out.println("Beginner");
          return new BeginnerPlayer(startCash);
        }
        else
        {
          System.out.println("Experienced");
          return new ExperiencedPlayer(startCash);
        }
      }
      else if(level == 2)
      {
        if(rand <= 4)
        {
          System.out.println("Beginner");
          return new BeginnerPlayer(startCash);
        }
        else if(rand >= 5 && rand <= 7)
        {
          System.out.println("Experienced");
          return new ExperiencedPlayer(startCash);
        }
        else if(rand >= 8 && rand <= 9)
        {
          System.out.println("SemiPro");
          return new SemiProPlayer(startCash, numDecks);
        }
        else
        {
          System.out.println("Pro");
          return new ProPlayer(startCash, numDecks);
        }
      }
      else
      {
        if(rand <= 4)
        {
          System.out.println("Experienced");
          return new ExperiencedPlayer(startCash);
        }
        else if(rand >= 5 && rand <= 7)
        {
          System.out.println("SemiPro");
          return new SemiProPlayer(startCash, numDecks);
        }
        else if(rand >= 8 && rand <= 9)
        {
          System.out.println("Pro");
          return new ProPlayer(startCash, numDecks);
        }
        else
        {
          System.out.println("Cheater");
          return new CheaterPlayer(startCash);
        }
      }
  }
  /**
   * A method that Deals cards to all of the players, if they are still in the game
   */
  public void Dealing() 
  {
    for(int i = 0; i < 2; i++)
    {
      if(cpu1Status)
        cpu1.hands[0][i] = deck.draw();
      if(cpu2Status)
        cpu2.hands[0][i] = deck.draw();
      if(cpu3Status)
        cpu3.hands[0][i] = deck.draw();
      if(cpu4Status)
        cpu4.hands[0][i] = deck.draw();
      player.hands[0][i] = deck.draw();
      dealer.hands[0][i] = deck.draw();
    }
  }
  /**
   * A method that lets the AI make bets
   * @param cpu
   */
  public void AIBetting(AIPlayer cpu)
  {
    cpu.startHand(deck);
  }
  /**
   * A method that lets the AI make splits
   * @param deck
   * @param dealer
   * @param cpu
   */
  public void AISplit(GameDeck deck, Dealer dealer, AIPlayer cpu) 
  {
    Action action;
    for(int i = 0; i < 3; i++)
    {
      //if the AI would like to split its hand
      action = cpu.getMove(deck, i, dealer.getHoleCard());
      if(action == Action.SPLIT)
      {
        System.out.println("Splits hand " + (i + 1) + ".");
        cpu.play(deck, i, action);
        cpu.hand1DidnotSplit = false;
      }
      //exit the loop otherwise
      else
      {
        i = 3;
      }
    }
    //all of the hands are now post split.
    cpu.hand1DidnotSplit = false;
    cpu.hand2DidnotSplit = false;
    cpu.hand3DidnotSplit = false;
    cpu.hand4DidnotSplit = false;
  }
  /**
   * A method that lets the AIs play their hands
   * @param GameDeck
   * @param cpu
   * @param dealer
   * @param handCounter
   */
  public void AITurn(GameDeck GameDeck, AIPlayer cpu, Dealer dealer, int handCounter) 
  {
    Action action;
    action = cpu.getMove(GameDeck, handCounter, dealer.getHoleCard());
    
    if(action == Action.DOUBLE_DOWN)
    {
      System.out.println(action.toString() + "s on hand " + (handCounter + 1) + ".");
      cpu.play(GameDeck, handCounter, action);
      cpu.play(GameDeck, handCounter, Action.STAND);
    }
    else
    {
      while(cpu.canStand(handCounter))
      {
        cpu.play(GameDeck, handCounter, action);
        System.out.println(action.toString() + "s on hand " + (handCounter + 1) + ".");
        action = cpu.getMove(GameDeck, handCounter, dealer.getHoleCard());
      }
    }
  }
  /**
   * A method that handles the payouts of the AI hands
   * @param cpu
   * @param dealer
   * @param numCpu
   */
  public void AIPayout(AIPlayer cpu, Dealer dealer, int numCpu) 
  {
    for(int handCounter = 1; handCounter < 5; handCounter++)
    {
      if(cpu.hands[handCounter - 1][0] != null) 
      {
        if(!cpu.handValue(0).equals("BLACKJACK"))
        { 
          if(((cpu.sum(handCounter - 1) > dealer.sum(0)) && 
             (cpu.sum(handCounter - 1) <= 21)) ||
             ((cpu.sum(handCounter - 1) <= 21) && ((dealer.sum(0) > 21))))
          {
            System.out.print("Hand " + handCounter + " won! CPU" + numCpu + " won $" + (2 * cpu.getBet(handCounter - 1)));
//            cpu.setMoney(cpu.getMoney() + 2 * cpu.getBet(handCounter - 1));
            cpu.wonRound(handCounter - 1);
          }
          else if (((dealer.sum(0) > cpu.sum(handCounter - 1)) && 
                    (dealer.sum(0) <= 21)) ||
                   ((dealer.sum(0) <= 21) && ((cpu.sum(handCounter - 1) > 21))))
          {
            System.out.print("Hand " + handCounter + " lost! CPU" + numCpu + " lost $" + cpu.getBet(handCounter - 1));
            cpu.lostRound(handCounter - 1);
          }
          else
          {
            System.out.print("Hand " + handCounter + " tied!");
//            player.setMoney(cpu.getMoney() + cpu.getBet(handCounter - 1));
            cpu.tiedRound(handCounter - 1);
          }
        }
        //Payout for cpu BLACKJACK is 3:2
        //if Dealer also has BLACKJACK, then it is a tie
        else
        {
          if(dealer.handValue(0).equals("BLACKJACK")) {
            System.out.println("Dealer Also has BLACKJACK!");
            System.out.println("Hand " + handCounter + " tied!");
            cpu.tiedRound(handCounter - 1);
          } else {
            System.out.println("Hand " + handCounter + " won! CPU" + numCpu + " won $" + (3 * cpu.getBet(handCounter - 1) / 2));
            cpu.wonRound(handCounter - 1);
            cpu.setMoney(cpu.getMoney() + (cpu.getBet(handCounter - 1) / 2));
          }
        } 
        System.out.print("     Enter anything to continue.");
        spacer = scan.nextLine();
      }
    }
  }
  /**
   * Dealer's turn
   * @param dealer
   */
  public void DealerTurn(Dealer dealer) 
  {
    boolean done;
    Action Action;
    
    done = false;
    Action = null;
    
    // Show all cards that the dealer has
    dealer.setShowCards(true);
    System.out.println("Dealer's Hand: ");
    System.out.print(dealer.getCards());
    System.out.print("     Enter anything to continue.");
    spacer = scan.nextLine();
    while(!done){
      
      //loop until the dealer stands
      while(Action != Action.STAND)
      {
        Action = dealer.getMove(deck, 1 - 1, dealer.hands[0][1]);
        dealer.play(deck, 1 - 1, Action);
        System.out.println("Dealer " + Action + "s");
        if(Action != Action.STAND)
        {
                  System.out.print(dealer.getCards());
        System.out.print("     Enter anything to continue.");
        spacer = scan.nextLine();
        }
      }
      done = true;
    }
  }
  /**
   * Player betting
   * @param player
   */
  public void PlayerBetting(Player player) 
  {
    boolean bet = false;
    while(!bet)
    {
      try
      {
        System.out.println("Place your bet: ");
        if(player.getMoney() < 100)
        {
          System.out.println("You can bet up to $" + (player.getMoney() - (player.getMoney() % 10)) + ".");
        } 
        else 
        {
          System.out.println("You can bet up to $100.");
        }
        playerBet = scan.nextInt();
        if(playerBet > 100)
        {
          System.out.println("You cannot bet more than $100.");
        }
        else if(playerBet < 10)
        {
          System.out.println("You cannot bet less than $10.");
        }
        else if(playerBet % 10 != 0)
        {
          System.out.println("You can only bet multiples of 10.");
        }
        else
        {
          player.setBet(0, playerBet);
          bet = true;
        }
      }
      catch (InputMismatchException ime)
      {
        System.out.println("Please only enter Integers.");
      }
    }
  }
  
  public void PlayerSplit(GameDeck GameDeck, Dealer Dealer, Player player) 
  {
    Scanner s2 = new Scanner(System.in);
    int InitialBet = player.getBet(0);
    Choice = "";
    //Checks if the player can split the first hand.
    if(player.canSplit(1 - 1))
    {
      System.out.println("Would you like to split your first hand? Y/N: ");
      Choice = s2.nextLine();
      while(!Choice.equalsIgnoreCase("Q"))
      {
        if(Choice.equalsIgnoreCase("Y"))
        {
          
          //If the player does not have enough money to place down,
          //then the player cannot split.
          if(player.canBet(InitialBet))
          {
            player.setBet(2 - 1, InitialBet);
            player.play(GameDeck, 1 - 1, Action.SPLIT);
            System.out.println("You splitted your first hand.");
            System.out.println(player.getCards());
          }
          //displayDealerCovered(Dealer, Player);
            
          //Checks if the player can split the new hand (second hand).
          if(player.canSplit(2 - 1))
          {
            System.out.println("Would you like to split your second hand? Y/N");
            while(!Choice.equalsIgnoreCase("Q"))
            {
              Choice = s2.nextLine();
              if(Choice.equalsIgnoreCase("Y"))
              {
                //If the player does not have enough money to place down,
                //then the player cannot split.
                if(player.canBet(InitialBet))
                {
                  player.setBet(3 - 1, InitialBet);
                  player.play(deck, 2 - 1, Action.SPLIT);
                  System.out.println("You splitted your second hand.");
                  System.out.println(player.getCards());
                }
                //displayDealerCovered(Dealer, Player);
                 
                //Checks if the player can split the new hand (third hand).
                if(player.canSplit(3 - 1))
                {
                  System.out.println("Would you like to split your third hand? Y/N");
                  while(!Choice.equalsIgnoreCase("Q"))
                  {
                    Choice = s2.nextLine();
                    if(Choice.equalsIgnoreCase("Y"))
                    {
                      //If the player does not have enough money to place down,
                      //then the player cannot split.
                      if(player.canBet(InitialBet))
                      {
                        player.setBet(4 - 1, InitialBet);
                        player.play(deck, 3 - 1, Action.SPLIT);
                        System.out.println("You splitted your third hand.");
                        System.out.println(player.getCards());
                      }
                      Choice = "Q";
                      //displayDealerCovered(Dealer, Player);
                    }
                    else if(Choice.equalsIgnoreCase("N"))
                    {
                      System.out.println("You have not split your third hand.");
                      Choice = "Q";
                      //displayDealerCovered(Dealer, Player);
                    }
                    else
                    {
                      System.out.println("Invalid Option!");
                      Choice = "";
                      Choice = s2.nextLine();
                      //displayDealerCovered(Dealer, Player);
                    }
                  }
                }
                Choice = "Q";
              }
              else if(Choice.equalsIgnoreCase("N"))
              {
                System.out.println("You have not split your second hand.");
                Choice = "Q";
                //displayDealerCovered(Dealer, Player);
              }
              else
              {
                System.out.println("Invalid Option!");
                Choice = "";
                Choice = s2.nextLine();
                //displayDealerCovered(Dealer, Player);
              }
            }
          }
          Choice = "Q";
        }
        else if(Choice.equalsIgnoreCase("N"))
        {
          System.out.println("You have not split your first hand.");
          Choice = "Q";
          //displayDealerCovered(Dealer, Player);
        }
        else
        {
          System.out.println("Invalid Option!");
          Choice = "";
          Choice = s2.nextLine();
          //displayDealerCovered(Dealer, Player);
        }
      }
    }
  }
  
  public void PlayerTurn(GameDeck deck, Player player, int handCounter) 
  {
    String userin = "";
    int InitialBet = player.getBet(1 - 1);
    System.out.println("");
    //Display the available options of the hand that the player can make.
    while(player.canStand(handCounter - 1))
    {
      System.out.print(player.getCards());
      if(!player.handValue(handCounter - 1).equals("BLACKJACK") 
         && !player.handValue(handCounter - 1).equals("BUSTED") 
         && player.sum(handCounter - 1) != 21) 
      {
        System.out.println("");
        System.out.print("Please enter your Action for Hand " + handCounter + ": ");
        if(player.canHit(handCounter - 1))
        {
          System.out.print("Hit(H) ");
          if(player.canStand(handCounter - 1))
          {
            System.out.print("Stand(S) ");
            if(player.canDoubleDown(handCounter - 1))
            {
              System.out.print("DoubleDown(D) ");
            }
          }
        }
        userin = "";
        if(player.handValue(handCounter - 1).equals("BUSTED"))
        {
          userin = "s";
        }
        else
        {
          userin = scan.nextLine();
        }
        //hit
        if(userin.equalsIgnoreCase("hit") || userin.equalsIgnoreCase("h"))
        {
          if(player.canHit(handCounter - 1))
          {
            player.play(deck, handCounter - 1, Action.HIT);
            System.out.println("You chose to Hit.");
          }
          else
          {
            System.out.println("You can no longer Hit on hand " + handCounter);
          }
        }
        
        //stand
        else if(userin.equalsIgnoreCase("stand") || userin.equalsIgnoreCase("s"))
        {
          if(player.canStand(handCounter - 1))
          {
            player.play(deck, handCounter - 1, Action.STAND);
            System.out.println("You chose to Stand.");
          }
          else
          {
            System.out.println("You can no longer Stand on hand " + handCounter);
          }
        }
        
        //double down
        else if(userin.equalsIgnoreCase("doubledown") || userin.equalsIgnoreCase("d"))
        {
          if(player.canDoubleDown(handCounter - 1))
          {
            
            //checks if the player has enough money to doubledown
            if(player.canBet(InitialBet))
            {
              player.play(deck, handCounter - 1, Action.DOUBLE_DOWN);
              System.out.println("You chose to DoubleDown.");
              // BRANDEN: Temporarily removed this because the betting for doubling down
              // is already handled within the Seat class
              /*
              player.setBet(handCounter - 1, 2 * InitialBet);
              */
            }
            else
            {
              System.out.println("You do not have enough cash.");
            }
          }
          else
          {
            System.out.println("You can no longer DoubleDown on hand " + handCounter);
          }
        }
      }
      else 
      {
        player.play(deck, handCounter - 1, Action.STAND);
        if(player.handValue(handCounter - 1).equals("BLACKJACK"))
        {
          System.out.println("You have BLACKJACK!");
        }
        else if(player.handValue(handCounter - 1).equals("BUSTED"))
        {
          System.out.println("Hand " + handCounter + " BUSTED!");
        }
      }
      System.out.println("");
    }
  }
  /**
   * A method that handles the payout for the player
   * @param player
   * @param dealer
   */
  public void PlayerPayout(Player player, Dealer dealer) 
  {
    for(int handCounter = 1; handCounter < 5; handCounter++)
    {
      if(player.hands[handCounter - 1][0] != null) 
      {
        if(!player.handValue(0).equals("BLACKJACK"))
        { 
          if(((player.sum(handCounter - 1) > dealer.sum(0)) && 
             (player.sum(handCounter - 1) <= 21)) ||
             ((player.sum(handCounter - 1) <= 21) && ((dealer.sum(0) > 21))))
          {
            System.out.print("Hand " + handCounter + " won! You won $" + (2 * player.getBet(handCounter - 1)));
//            player.setMoney(player.getMoney() + 2 * player.getBet(handCounter - 1));
            player.wonRound(handCounter - 1);
          }
          else if (((dealer.sum(0) > player.sum(handCounter - 1)) && 
                    (dealer.sum(0) <= 21)) ||
                   ((dealer.sum(0) <= 21) && ((player.sum(handCounter - 1) > 21))))
          {
            System.out.print("Hand " + handCounter + " lost! You lost $" + player.getBet(handCounter - 1));
            player.lostRound(handCounter - 1);
          }
          else
          {
            System.out.print("Hand " + handCounter + " tied!");
            player.setMoney(player.getMoney() + player.getBet(handCounter - 1));
            player.tiedRound(handCounter - 1);
          }
        }
        //Payout for player BLACKJACK is 3:2
        //if Dealer also has BLACKJACK, then it is a tie
        else
        {
          if(dealer.handValue(0).equals("BLACKJACK")) {
            System.out.println("Dealer Also has BLACKJACK!");
            System.out.println("Hand " + handCounter + " tied!");
            player.tiedRound(handCounter - 1);
          } else {
            System.out.println("Hand " + handCounter + " won! You won $" + (3 * player.getBet(handCounter - 1) / 2));
            player.wonRound(handCounter - 1);
            player.setMoney(player.getMoney() + (player.getBet(handCounter - 1) / 2));
          }
        } 
        System.out.print("     Enter anything to continue.");
        spacer = scan.nextLine();
      }
    }
  }
  /**
   * A method that displays all of the player's hands
   */
  public void DisplayHands()
  {
    System.out.println("Dealer hand: ");
    System.out.print(dealer.getCards());
    System.out.println("AI1 hand: ");
    System.out.print(cpu1.getCards());
    System.out.println("AI2 hand: ");
    System.out.print(cpu2.getCards());
    System.out.println("AI3 hand: ");
    System.out.print(cpu3.getCards());
    System.out.println("AI4 hand: ");
    System.out.print(cpu4.getCards());
    System.out.println("Your hand: ");
    System.out.print(player.getCards());
  }
  /**
   * A method that displays All of the cash of the players
   */
  public void DisplayMoney()
  {
    if(cpu1Status)
      System.out.println("cpu1 has $" + cpu1.getMoney() + ".");
    if(cpu2Status)
      System.out.println("cpu2 has $" + cpu2.getMoney() + ".");
    if(cpu3Status)
      System.out.println("cpu3 has $" + cpu3.getMoney() + ".");
    if(cpu4Status)
      System.out.println("cpu4 has $" + cpu4.getMoney() + ".");
    System.out.println("You have $" + player.getMoney() + ".");
  }
  /**
   * A method that clears the table for the next round
   */
  public void ClearTable() {
    /**
     * Enables splitting again
     */
    cpu1.hand1DidnotSplit = true;
    cpu1.hand2DidnotSplit = true;
    cpu1.hand3DidnotSplit = true;
    cpu2.hand1DidnotSplit = true;
    cpu2.hand2DidnotSplit = true;
    cpu2.hand3DidnotSplit = true;
    cpu3.hand1DidnotSplit = true;
    cpu3.hand2DidnotSplit = true;
    cpu3.hand3DidnotSplit = true;
    cpu4.hand1DidnotSplit = true;
    cpu4.hand2DidnotSplit = true;
    cpu4.hand3DidnotSplit = true;
    /**
     * clear the table
     */
    cpu1.clear();
    cpu2.clear();
    cpu3.clear();
    cpu4.clear();
    player.clear();
    dealer.clear();
  }
  
}
